using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using AutoMapper;
using HuiLian.Common.Input;
using HuiLian.Common.Output;
using HuiLian.Model.Platform;
using HuiLian.Repository.Platform;
using HuiLian.Service.Platform.Sponsorship.Input;
//using HuiLian.Service.Platform.Sponsorship.Output;
using HuiLian.Common.Attributes;
using HuiLian.Common;

namespace HuiLian.Service.Platform.Sponsorship
{
    /// <summary>
    /// 协办单服务
    /// </summary>
    public class SponsorshipService : ISponsorshipService
    {
        private readonly IMapper _mapper;
        private readonly ISponsorshipRepository _sponsorshipRepository;

        public SponsorshipService(IMapper mapper, ISponsorshipRepository sponsorshipRepository)
        {
            _mapper = mapper;
            _sponsorshipRepository = sponsorshipRepository;
        }

        /// <summary>
        /// 获取协办单信息
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<IResponseOutput> GetAsync(long id)
        {
            var result = await _sponsorshipRepository.GetAsync(id);
            return ResponseOutput.Ok(result);
        }

        /// <summary>
        /// 查询协办单信息
        /// </summary>
        /// <param name="input">分页信息</param>
        /// <returns></returns>
        public async Task<IResponseOutput> PageAsync(PageInput<SponsorshipEntity> input)
        {
            //var key = input.Filter?.Label;

            var list = await _sponsorshipRepository.Select
            .WhereDynamicFilter(input.DynamicFilter)
            //.WhereIf(key.NotNull(), a => a.Path.Contains(key) || a.Label.Contains(key))
            .Count(out var total)
            .OrderBy(true, c => c.Id)
            .Page(input.CurrentPage, input.PageSize)
            .ToListAsync();

            var data = new PageOutput<SponsorshipEntity>()
            {
                List = list,
                Total = total
            };

            return ResponseOutput.Ok(data);
        }

        /// <summary>
        /// 添加协办单信息
        /// </summary>
        /// <param name="input">协办单信息</param>
        /// <returns></returns>
        public async Task<IResponseOutput> AddAsync(SponsorshipAddInput input)
        {
            var entity = _mapper.Map<SponsorshipEntity>(input);
            var id = (await _sponsorshipRepository.InsertAsync(entity)).Id;

            return ResponseOutput.Result(id > 0);
        }

        /// <summary>
        /// 更新协办单信息
        /// </summary>
        /// <param name="input">协办单信息</param>
        /// <returns></returns>
        public async Task<IResponseOutput> UpdateAsync(SponsorshipUpdateInput input)
        {
            if (!(input?.Id > 0))
            {
                return ResponseOutput.NotOk();
            }

            var entity = await _sponsorshipRepository.GetAsync(input.Id);
            if (!(entity?.Id > 0))
            {
                return ResponseOutput.NotOk("数据不存在！");
            }

            _mapper.Map(input, entity);
            await _sponsorshipRepository.UpdateAsync(entity);
            return ResponseOutput.Ok();
        }

        /// <summary>
        /// 删除协办单信息
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<IResponseOutput> DeleteAsync(long id)
        {
            var result = false;
            if (id > 0)
            {
                result = (await _sponsorshipRepository.DeleteAsync(m => m.Id == id)) > 0;
            }

            return ResponseOutput.Result(result);
        }

        /// <summary>
        /// 软删除协办单信息
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<IResponseOutput> SoftDeleteAsync(long id)
        {
            var result = await _sponsorshipRepository.SoftDeleteAsync(id);
            return ResponseOutput.Result(result);
        }

        /// <summary>
        /// 批量软删除协办单信息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<IResponseOutput> BatchSoftDeleteAsync(long[] ids)
        {
            var result = await _sponsorshipRepository.SoftDeleteAsync(ids);

            return ResponseOutput.Result(result);
        }

        /// <summary>
        /// 添加列表
        /// </summary>
        /// <param name="sponsorships"></param>
        /// <returns></returns>
        public async Task<IResponseOutput> AddListAsync(List<SponsorshipEntity> sponsorships)
        {
            var entitys = await _sponsorshipRepository.Select.ToListAsync();
            List<SponsorshipEntity> addList = new List<SponsorshipEntity>();
            foreach (var sponsorship in sponsorships)
            {
                var entity = entitys.Where(p => p.序号 == sponsorship.序号 && p.主办单编号 == sponsorship.主办单编号 && p.协办单编号 == sponsorship.协办单编号).FirstOrDefault();
                if (entity == null) addList.Add(sponsorship);
            }

            if (addList.Count > 0)
            {
                var count = (await _sponsorshipRepository.InsertAsync(addList)).Count;
                return ResponseOutput.Result(count > 0);
            }
            return ResponseOutput.Result(true);
        }

        /// <summary>
        /// 更新时间
        /// </summary>
        /// <param name="sponsorships"></param>
        /// <returns></returns>
        public async Task<IResponseOutput> UpdateTime(List<SponsorshipEntity> sponsorships)
        {
            var entitys = await _sponsorshipRepository.Select.Where(p => p.协办单编号 != "WS20211105000340").ToListAsync();
            foreach (var entity in entitys)
            {
                var sponsorship = sponsorships.Where(p => /*p.序号 == entity.序号 && */p.主办单编号 == entity.主办单编号 && p.协办单编号 == entity.协办单编号 && p.协办接单人 == entity.协办接单人).FirstOrDefault();
                if(sponsorship != null)
                {
                    if (string.IsNullOrEmpty(entity.序号))
                    {

                    }
                    entity.序号 = sponsorship.序号;
                    entity.主办单接单时间 = sponsorship.主办单接单时间;
                    entity.协办接单时间 = sponsorship.协办接单时间;
                    entity.协办处理时间 = sponsorship.协办处理时间;
                    entity.销单时间 = sponsorship.销单时间;
                }
            }
            await _sponsorshipRepository.UpdateAsync(entitys);
            return ResponseOutput.Result(true);
        }

    }
}
